package com.fgr.controller;

import com.fgr.beans.Reserve;
import com.fgr.beans.User;
import com.fgr.mapper.UserMapper;
import com.fgr.service.ReserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ReserveController {
    @Autowired
    ReserveService reserveService;

    @Autowired
    UserMapper userMapper;

    @PostMapping("GetReserveType")
    @ResponseBody
    public String DetermineLoginReserve(String reType,HttpSession httpSession){
        Object obj = httpSession.getAttribute("userid");
        if (obj==null){
            return "Login";
        }
        httpSession.setAttribute("reType",reType);

        return "SUCCESS";

    }


    @PostMapping("addReserve")
    @ResponseBody
    public String addReserve(Reserve reserve){
        String s = reserveService.addReserve(reserve);
        return s;
    }

    @GetMapping("findReserveByTypeBg")
    public String findReserveByTypeBg(String reType,Model model,HttpSession httpSession){
        httpSession.setAttribute("reType",reType);
        List<Reserve> reserves = reserveService.findReserveByTypeBg(reType);
        model.addAttribute("reserves",reserves);
        return "background/help-reserve";
    }

    @GetMapping("findReserveByReIdAndUser")
    public String findReserveByReIdAndUser(Integer reId,Model model){
        Reserve reserve = reserveService.findReserveByReId(reId);
        List<User> users = userMapper.selectAllUser();
        model.addAttribute("reserve",reserve);
        model.addAttribute("users",users);

        return "background/modifyreserve";
    }

    @ResponseBody
    @PostMapping("deleteReserveByTypeBg")
    public String deleteReserveByTypeBg(Integer reId){

        String s = reserveService.deleteReserveByTypeBg(reId);
        return s;

    }
    @PostMapping("modifyReserve")
    @ResponseBody
    public String modifyReserve(Reserve reserve){
        String s = reserveService.UpdateReserve(reserve);
        return s;
    }

}
